import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/login";
import Layout from "../views/layout";
import homePage from "../views/layout/homePage/index.vue";
import Users from "../views/layout/user/users.vue";
import Rights from "../views/layout/right/rights.vue";
import Roles from "../views/layout/right/roles.vue";
import Reports from "../views/layout/report/reports.vue";
import Order from "../views/layout/order/orders.vue";
import Categories from "../views/layout/good/categories.vue";
import Params from "../views/layout/good/params.vue";
import Goods from "../views/layout/good/goods.vue";
import Add from "../views/layout/good/add.vue";
import store from "@/store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/homepage",
    children: [
      {
        path: "homepage",
        component: homePage,
      },
      {
        path: "users",
        component: Users,
      },
      {
        path: "rights",
        component: Rights,
      },
      {
        path: "roles",
        component: Roles,
      },
      {
        path: "reports",
        component: Reports,
      },
      {
        path: "orders",
        component: Order,
      },
      {
        path: "categories",
        component: Categories,
      },
      {
        path: "params",
        component: Params,
      },
      {
        path: "goods",
        component: Goods,
      },
      {
        path: "add",
        component: Add,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  if (token || to.path === "/login") {
    next();
  } else {
    next("/login");
  }
});
export default router;
